﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNet.Common.Command;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using NHibernate;
using ZLCMS.Commands.Sys.Commands.Order;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.PayChannal.Code
{
    public abstract class PaymentCallbackBase : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
        }

        /// <summary>
        /// 支付信息
        /// </summary>
        protected ShopPaymentInfo PaymentInfo { get; set; }

        /// <summary>
        /// 支付编码
        /// </summary>
        protected string PaymentNo { get; set; }


        protected void ReportMessage(string msg)
        {
            Response.Write(msg);
            Response.End();
        }

        /// <summary>
        /// 确认支付
        /// </summary>
        /// <returns></returns>
        protected bool ConfirmPayment(decimal paymentfee, string paymentNo, string tradeNo)
        {
            ICommandResult<PayOrderResult> result = new PayOrderCommand().Execute(new PayOrderParameter() { PaymentPrice = paymentfee, PaymentNo = paymentNo, TradeNo = tradeNo });
                          
            //todo记录支付日志
            if (!result.Error)
            {
                CMSKits.WriteLog("system", "支付成功", "paymentNo=" + paymentNo + ",tradeNo=" + tradeNo + ",totalprice=" + paymentfee);
            }
            else
            {
                CMSKits.WriteLog("system", "支付失败", "paymentNo=" + paymentNo + ",tradeNo=" + tradeNo + ",totalprice=" + paymentfee);
            }

            return result.Error;
        }

        /// <summary>
        /// 初始化请求数据，设置paymentinfo
        /// </summary>
        protected abstract void Initialize();

        /// <summary>
        /// 判断回调签名
        /// </summary>
        /// <returns></returns>
        protected abstract bool CheckSign();
    }
}